package com.wish.group05.financial.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wish.group05.base.entity.JrProduct;
import com.wish.group05.base.entity.MBank;
import com.wish.group05.base.entity.SysRegion;
import com.wish.group05.base.service.IJrProductService;
import com.wish.group05.base.service.IMBankService;
import com.wish.group05.base.service.ISysRegionService;
import com.wish.group05.common.security.utils.SecurityUtils;
import com.wish.group05.system.api.RemoteSmsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.wish.group05.common.log.annotation.Log;
import com.wish.group05.common.log.enums.BusinessType;
import com.wish.group05.common.security.annotation.PreAuthorize;
import com.wish.group05.common.core.web.controller.BaseController;
import com.wish.group05.common.core.web.domain.AjaxResult;
import com.wish.group05.common.core.utils.poi.ExcelUtil;
import com.wish.group05.common.core.web.page.TableDataInfo;

/**
 * 【请填写功能名称】Controller
 * 
 * @author wish
 * @date 2022-05-26
 */
@RestController
@RequestMapping("/product")
public class TJrProductController extends BaseController
{
    @Autowired
    private IJrProductService iJrProductService;

    @Autowired
    ISysRegionService iSysRegionService;

    @Autowired
    IMBankService imBankService;
    /**
     * 查询【请填写功能名称】列表
     */
//    @PreAuthorize(hasPermi = "financial:product:list")
    @GetMapping("list")
    public AjaxResult list(JrProduct jrProduct)
    {
        startPage();
        List<JrProduct> rentingList = iJrProductService.list(new QueryWrapper<JrProduct>().eq("jr_product_type",1).eq("auditing",1));
        List<JrProduct> homeList = iJrProductService.list(new QueryWrapper<JrProduct>().eq("jr_product_type",2).eq("auditing",1));
        List<JrProduct> receivableList = iJrProductService.list(new QueryWrapper<JrProduct>().eq("jr_product_type",3).eq("auditing",1));
        HashMap<Object, Object> map = new HashMap<>();
        map.put("rentingList",rentingList);
        map.put("homeList",homeList);
        map.put("receivableList",receivableList);

        return AjaxResult.success(map);
    }
    @GetMapping("auditingList")
    public TableDataInfo auditingList(JrProduct jrProduct)
    {

        startPage();
        List<JrProduct> list = iJrProductService.list(new QueryWrapper<JrProduct>().eq("auditing",0));
        return getDataTable(list);


    }

    /**
     * 删除【请填写功能名称】
     */
    @PreAuthorize(hasPermi = "system:product:remove")
    @Log(title = "作废软删除", businessType = BusinessType.DELETE)
	@DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
        JrProduct jrProduct = iJrProductService.getById(id);
        jrProduct.setDelFlg(false);
        boolean orUpdate = iJrProductService.saveOrUpdate(jrProduct);

        return toAjax(orUpdate==true?1:0);
    }





    /**
     * 新增【请填写功能名称】
     */
    @PreAuthorize(hasPermi = "system:product:add")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody JrProduct tJrProduct)
    {
        //如果是修改 审核状态赋值为通过
        if(tJrProduct.getId()!=null){
            tJrProduct.setAuditing(1);
        }else{
            tJrProduct.setAuditing(0);
        }
       tJrProduct.setDelFlg(true);
        MBank mBank = imBankService.getById(tJrProduct.getBankId());
        tJrProduct.setBankName(mBank.getBankName());
        //获取当前登录对象的 创建时间和id
        tJrProduct.setCreateTime(new Date());
        tJrProduct.setCreateUserid(SecurityUtils.getUserId());
        tJrProduct.setUpdateTime(new Date());
        tJrProduct.setUpdateUserid(SecurityUtils.getUserId());
        boolean save = iJrProductService.saveOrUpdate(tJrProduct);
        return toAjax(save==true?1:0);
    }

    /**
     * 修改【请填写功能名称】
     */
//    @PreAuthorize(hasPermi = "system:product:edit")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody JrProduct jrProduct)
    {

        boolean flag = iJrProductService.saveOrUpdate(jrProduct);
        return toAjax(flag==true?1:0);
    }

    //查询地区表
    @GetMapping("getCity")
    public AjaxResult getCity(){
        //查询省
        List<SysRegion> city = iSysRegionService.list(new QueryWrapper<SysRegion>().eq("REGION_LEVEL", 1));

        return AjaxResult.success(city);
    }

    //查询银行
    @GetMapping("getBank")
    public AjaxResult getBank(){
        List<MBank> list = imBankService.list();
        return AjaxResult.success(list);
    }


}
